package com.imot.scheduler;

import com.imot.pojo.entity.EmaRecord;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Random;

/**
 * ClassName: DataPushSchedule
 * Description:
 * date: 2023/11/22 14:15
 *
 * @author imot-jiah.wang
 */
@Component
@RequiredArgsConstructor
public class DataPushSchedule {
    private static final Random random = new Random();
    @Scheduled(fixedRate = 1000)
    public void datapush(){
        EmaRecord emaRecord = new EmaRecord();
        // 温度: -20.00 到 150.00
        emaRecord.setTemperature(randomFloatInRange(-20.00f, 150.00f));

        // 速度: -10 到 10
        emaRecord.setSpeed(randomFloatInRange(-10f, 10f));

        // 加速度: 0.00 到 5.00
        emaRecord.setAcceleration(randomFloatInRange(0f, 5f));

        // 电源电压: 22.00 到 32.00
        emaRecord.setSupplyVoltage(randomFloatInRange(22f, 32f));

        // 电机电流: 0.00 到 42.00
        emaRecord.setMotorCurrent(randomFloatInRange(0f, 42f));

        // 电机转速: -5000 到 5000
        emaRecord.setMotorSpeed(randomFloatInRange(-5000f, 5000f));

        // 位移: -80.00 到 80.00
        emaRecord.setDisplacement(randomFloatInRange(-80f, 80f));

        // 推力: -5000.00 到 5000.00
        emaRecord.setThrust(randomFloatInRange(-5000f, 5000f));

        // 电磁扭矩: -100 到 100
        emaRecord.setElecTorque(randomFloatInRange(-100f, 100f));

        // 时间: 当前时间
        emaRecord.setTime(LocalDateTime.now());

        emaRecord.setDeviceName("机电作动器");

    }

    private static float randomFloatInRange(float min, float max) {
        return min + random.nextFloat() * (max - min);
    }
}

